/**
 * Created by jianglinjie on Mon Oct 09 2017
 * 
 */
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _RegisterType = require('../../constants/RegisterType');

var RegisterType = _interopRequireWildcard(_RegisterType);

var _network = require('../../network');

var _config = require('../config');

var _config2 = _interopRequireDefault(_config);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var REGISTER_PATH = '/users';

var RegisterService = function () {
  function RegisterService() {
    _classCallCheck(this, RegisterService);
  }

  _createClass(RegisterService, [{
    key: 'registerWithUserName',
    value: function () {
      var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(userName, pwd) {
        var param, userInfo;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                param = {
                  userName: userName,
                  pwd: pwd
                };
                _context.next = 3;
                return this.register(RegisterType.USERNAME_PWD, param);

              case 3:
                userInfo = _context.sent;
                return _context.abrupt('return', userInfo);

              case 5:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function registerWithUserName(_x, _x2) {
        return _ref.apply(this, arguments);
      }

      return registerWithUserName;
    }()
  }, {
    key: 'registerWithPhone',
    value: function () {
      var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(phone, pwd, captcha) {
        var param, userInfo;
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                param = {
                  phone: phone,
                  pwd: pwd,
                  captcha: captcha
                };
                _context2.next = 3;
                return this.register(RegisterType.PHONE_PWD_CAPTCHA, param);

              case 3:
                userInfo = _context2.sent;
                return _context2.abrupt('return', userInfo);

              case 5:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this);
      }));

      function registerWithPhone(_x3, _x4, _x5) {
        return _ref2.apply(this, arguments);
      }

      return registerWithPhone;
    }()
  }, {
    key: 'register',
    value: function () {
      var _ref3 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee3(type, param) {
        var request, body;
        return regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                request = new _network.JSONRequest(_config2.default.apiBaseHost + REGISTER_PATH, { type: type }, param);
                _context3.next = 3;
                return _network.FetchClient.start(request);

              case 3:
                body = _context3.sent;
                return _context3.abrupt('return', body.data);

              case 5:
              case 'end':
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

      function register(_x6, _x7) {
        return _ref3.apply(this, arguments);
      }

      return register;
    }()
  }]);

  return RegisterService;
}();

exports.default = new RegisterService();